package com.anjie.powerproject.config;

import com.anjie.powerproject.service.DeviceValueService;
import com.ql.util.express.ExpressRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * QL表达式执行器配置文件
 */
@Configuration
public class QLExpressConfig {

    @Autowired
    DeviceValueService deviceValueService;

    public boolean or(boolean a, boolean b) {
        return (a || b);
    }

    public boolean and(boolean a, boolean b) {
        return (a && b);
    }

    @Bean
    ExpressRunner runner() throws Exception {
        ExpressRunner runner = new ExpressRunner();
        addFunctions(runner);
        addMacros(runner);
        return runner;
    }

    /**
     * 定义自定义执行函数
     */
    private void addFunctions(ExpressRunner runner) throws Exception {
        runner.addFunctionOfServiceMethod("devVal", deviceValueService, "getDeviceValueByDevIdNow",
                new Class[]{Integer.class}, "获取点位数据失败");
        runner.addFunctionOfServiceMethod("devValByCode", deviceValueService, "getDeviceValueByCodeNow",
                new Class[]{String.class}, "获取点位数据失败");
        runner.addFunctionOfClassMethod("expOr", QLExpressConfig.class, "or",
                new Class[]{boolean.class, boolean.class}, "或运算失败");
        runner.addFunctionOfClassMethod("expAnd", QLExpressConfig.class, "and",
                new Class[]{boolean.class, boolean.class}, "与运算失败");
    }

    /**
     * 定义自定义宏
     */
    private void addMacros(ExpressRunner runner) throws Exception {

    }
}
